﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Pavilion.DesignerTool
{
    public class GameItemWrapper : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public string Name { get { return (GameItem == null) ? "{Null}" : GameItem.Name; } set { GameItem.Name = value; OnPropertyChanged("Name"); } }
        public GameItem GameItem { get; private set; }
        public List<GameDataWrapper> GameToolCollection { get; private set; }

        public GameItemWrapper(string gameItemName)
        {
            if (gameItemName != null)
                this.GameItem = GameItemManager.Instance[gameItemName];

            GameToolCollection = new List<GameDataWrapper>();
        }

        public void LoadGameTools(LoadNodeWrapper loadNodeWrapper)
        {
            foreach (GameData gameData in loadNodeWrapper.LoadNode.ObtainGameTools(Name))
                GameToolCollection.Add(new GameDataWrapper(gameData, loadNodeWrapper) { IsLoadItem = true });
        }

        public GameDataWrapper ObtainGameTool(string gameToolName)
        {
            return GameToolCollection.First(x => x.GameData.GameToolName == gameToolName);
        }

        public GameDataWrapper AddGameTool(string gameToolName, LoadNodeWrapper loadNodeWrapper)
        {
            GameData gameData = loadNodeWrapper.LoadNode.AddGameTool(GameItem.Name, gameToolName);
            GameDataWrapper gameDataWrapper = new GameDataWrapper(gameData, loadNodeWrapper) { IsLoadItem = true };

            GameToolCollection.Add(gameDataWrapper);
            GameToolTransfer.Add(loadNodeWrapper, this, gameToolName);

            return gameDataWrapper;
        }

        public void RemoveGameTool(string gameToolName, LoadNodeWrapper loadNodeWrapper)
        {
            loadNodeWrapper.LoadNode.RemoveGameTool(GameItem.Name, gameToolName);

            GameToolCollection.Remove(ObtainGameTool(gameToolName));
            GameToolTransfer.Remove(loadNodeWrapper, this, gameToolName);
        }

        public override string ToString() { return Name; }

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
